package co.yixiang.yshop.module.store.service.userbank;

import co.yixiang.yshop.module.store.dal.dataobject.storeshop.StoreShopDO;
import co.yixiang.yshop.module.store.dal.mysql.storeshop.StoreShopMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import co.yixiang.yshop.module.store.controller.admin.userbank.vo.*;
import co.yixiang.yshop.module.store.dal.dataobject.userbank.UserBankDO;
import co.yixiang.yshop.framework.common.pojo.PageResult;

import co.yixiang.yshop.module.store.convert.userbank.UserBankConvert;
import co.yixiang.yshop.module.store.dal.mysql.userbank.UserBankMapper;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.store.enums.ErrorCodeConstants.*;

/**
 * 提现账户 Service 实现类
 *
 * @author yshop
 */
@Service
@Validated
public class UserBankServiceImpl implements UserBankService {

    @Resource
    private UserBankMapper userBankMapper;
    @Resource
    private StoreShopMapper shopMapper;

    @Override
    public Long createUserBank(UserBankCreateReqVO createReqVO) {
        // 插入
        UserBankDO userBank = UserBankConvert.INSTANCE.convert(createReqVO);
        StoreShopDO storeShopDO = this.getShop(createReqVO.getShopId());
        userBank.setShopName(storeShopDO.getName());
        userBankMapper.insert(userBank);
        // 返回
        return userBank.getId();
    }

    @Override
    public void updateUserBank(UserBankUpdateReqVO updateReqVO) {
        // 校验存在
        validateUserBankExists(updateReqVO.getId());
        // 更新
        UserBankDO updateObj = UserBankConvert.INSTANCE.convert(updateReqVO);
        StoreShopDO storeShopDO = this.getShop(updateReqVO.getShopId());
        updateObj.setShopName(storeShopDO.getName());
        userBankMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserBank(Long id) {
        // 校验存在
        validateUserBankExists(id);
        // 删除
        userBankMapper.deleteById(id);
    }

    private void validateUserBankExists(Long id) {
        if (userBankMapper.selectById(id) == null) {
            throw exception(USER_BANK_NOT_EXISTS);
        }
    }

    @Override
    public UserBankDO getUserBank(Long id) {
        return userBankMapper.selectById(id);
    }

    @Override
    public List<UserBankDO> getUserBankList(Long shopId) {
        return userBankMapper.selectList(new LambdaQueryWrapper<UserBankDO>().eq(UserBankDO::getShopId,shopId));
    }

    @Override
    public PageResult<UserBankDO> getUserBankPage(UserBankPageReqVO pageReqVO) {
        return userBankMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UserBankDO> getUserBankList(UserBankExportReqVO exportReqVO) {
        return userBankMapper.selectList(exportReqVO);
    }


    /**
     * 获取门店
     * @param id
     * @return
     */
    private StoreShopDO getShop(Long id){
        //查找门店
        StoreShopDO storeShopDO = shopMapper.selectById(id);
        return  storeShopDO;
    }

}
